package dotaframework.game;

import dota.entity.ArmedUnit;
import dota.entity.ArmedUnitSoldier;
import dota.entity.Castle;
import gameframework.base.ObservableValue;
import gameframework.game.GameEntity;
import gameframework.game.GameUniverseDefaultImpl;
import gameframework.game.MoveBlockerChecker;
import gameframework.game.OverlapProcessor;

public class DotaGameUniverse extends GameUniverseDefaultImpl {

	protected ObservableValue<Integer> experience;
	protected ObservableValue<Integer> heroLevel;
	protected ObservableValue<Integer> gold;

	
	
	public DotaGameUniverse(MoveBlockerChecker obs, OverlapProcessor col, ObservableValue<Integer> experience, ObservableValue<Integer> heroLevel, ObservableValue<Integer> gold) {
		super(obs, col);
		this.experience = experience;
		this.heroLevel = heroLevel;
		this.gold = gold;
		
	}

	public void allAttackOnce() {

		for (GameEntity entity : gameEntities) {
			if (entity instanceof ArmedUnitSoldier) {
				if (((ArmedUnitSoldier) entity).alive()) {
					ArmedUnit blocker = ((ArmedUnitSoldier) entity).getBlocker();
					if (blocker != null) {
						if (blocker.getName().contains("teamBlue") != ((ArmedUnitSoldier) entity).getName().contains("teamBlue")) {
							
							float soldierAttack = ((ArmedUnitSoldier) entity).strike();
							float blockerAttack = blocker.strike();
							
							((ArmedUnitSoldier) entity).parry(blockerAttack);
							blocker.parry(soldierAttack);
							
							((ArmedUnitSoldier) entity).setBlocker(null);
							if (!blocker.alive()) {
								removeGameEntity((GameEntity) blocker);
								if ( blocker.getName().contains("teamRed") ){
									gold.setValue(gold.getValue()+10);
									experience.setValue(experience.getValue()+50);
								}
									
							}
							if (((ArmedUnitSoldier) entity).alive() == false) {
								removeGameEntity(entity);
								if ( ((ArmedUnitSoldier) entity).getName().contains("teamRed") ){
									gold.setValue(gold.getValue()+10);
									experience.setValue(experience.getValue()+50);
									if (blocker.getName().contains("Hero"))
									{
										System.out.println("Bonus Last Hit !");
										gold.setValue(gold.getValue()+50);
										experience.setValue(experience.getValue()+40);
									}
								}
							}
						}

					}
				}
			}
		}
	}

	public boolean checkEndGame() {
		boolean castleBlue = false;
		boolean castleRed = false;
		for (GameEntity entity : gameEntities) {
			if (entity instanceof Castle) {
				if (((Castle) entity).getName().contains("teamBlue"))
					castleBlue = true;
				if (((Castle) entity).getName().contains("teamRed"))
					castleRed = true;
			}

		}
		return !(castleBlue && castleRed);
	}

	public boolean isHeroAlive() {
		boolean isAlive = false;
		for (GameEntity entity : gameEntities) {
			if (entity instanceof ArmedUnitSoldier) {
				if (((ArmedUnitSoldier) entity).getName().contains("teamBlue-Hero"))
					isAlive = true;
			}

		}
		return isAlive;
	}
}
